package tech.aistar.corejava.day14;

import java.io.*;

/**
 * Created by 豆梦寒 on 2019/8/2.
 */
public class BufferedDemo {
    public static void main(String[] args) {
        copyFile("D:/测试3.txt","D:/测试3_副本.txt");
    }

    private static void copyFile(String src, String target) {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream(new FileOutputStream(target));


            byte[] buf = new byte[3*1024];

            while (true){
                int len = in.read(buf);
                if (len == -1){
                    break;
                }
                out.write(buf,0,len);
                System.out.println("文件拷贝成功");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}
